import { NextRequest, NextResponse } from "next/server";
import prisma from "../../../../lib/prisma";

export async function GET(
  request: NextRequest,
  { params }: { params: Promise<{ email: string }> }
) {
  try {
    const resolvedParams = await params;
    const { email } = resolvedParams;

    const user = await prisma.user.findUnique({
      where: { email },
      include: {
        chats: {
          select: {
            id: true,
            chatDate: true,
            chatTitle: true,
            userId: true,
          },
        },
      },
    });

    if (!user) {
      return NextResponse.json({ chats: [] });
    }

    return NextResponse.json({ user });
  } catch (error) {
    return NextResponse.json({ error: "Server error" }, { status: 500 });
  }
}
